package com.gopay.common.cps.manager.holiday.impl;

import com.gopay.common.constants.holiday.HolidayType;
import com.gopay.common.cps.dao.holiday.HolidayInformationDAO;
import com.gopay.common.cps.manager.holiday.HolidayInfomationUtilManager;
import com.gopay.common.domain.holiday.HolidayInformation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;

/** 
 * @fileName  HolidayInfomationManagerImpl.java
 * @author  张建龙 E-mail: 893718284@qq.com
 * @date 创建时间：2016年1月18日 下午3:21:34
 * @version 1.0
 * @parameter  
 * @since  
 * @return  
 */
@Service("holidayInformationUtilManager")
public class HolidayInfomationUtilManagerImpl implements HolidayInfomationUtilManager {
	
	
	@Resource(name="holidayInformationDAO")
	private HolidayInformationDAO holidayInfomationDao;
	
	/**
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return 是自动结算节假日返回true,否则返回false。
	 */
	@Override
	public boolean isAutoSettleHoliday(int year, int month,int dayOfMonth)  {
		return isHoliday(HolidayType.AUTO_SETTLE, year,month,dayOfMonth);
	}
	
	/**
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return 是自动结算节假日返回true,否则返回false。
	 */
	@Override
	public boolean isAutoSettleHoliday(String year, String month,String dayOfMonth)  {
		return isHoliday(HolidayType.AUTO_SETTLE, year,month,dayOfMonth);
	}
	
	/**
	 * @param date
	 * @return  是自动结算节假日返回true,否则返回false。
	 */
	@Override
	public boolean isAutoSettleHoliday(Date date)  {
		return isHoliday(HolidayType.AUTO_SETTLE, date);
	}
	
	/**
	 * @param date
	 * @return  是节假日返回true,否则返回false。
	 */
	@Override
	public boolean isHoliday(HolidayType holidayType,Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return isHoliday(holidayType, calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
	}
	
	/**
	 * @param holidayType
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return 是节假日返回true,否则返回false。
	 */
	@Override
	public boolean isHoliday(HolidayType holidayType, int year, int month,int dayOfMonth)  {
		return isHoliday(holidayType,String.valueOf(year),String.valueOf(month),String.valueOf(dayOfMonth));
	}
	
	/**
	 * @param holidayType
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return 是节假日返回true,否则返回false。
	 */
	@Override
	public boolean isHoliday(HolidayType holidayType, String year, String month,String dayOfMonth)  {
		String _month_1_based = String.valueOf(Integer.valueOf(month)+1);
		HolidayInformation holidayInformation = holidayInfomationDao.getHolidayInformationByYearandMonth(holidayType, year, _month_1_based);
		if(holidayInformation==null){
			return isCommonHoliday( year, month, dayOfMonth);
		}
		//Modified by ZhangYu3
		String[] holiday=holidayInformation.getHolidays().split(",");
		Assert.notNull(holiday, "holiday为空");
		for(int i=0;i<holiday.length;i++){
			if(holiday[i].trim().equals(dayOfMonth.trim())) {
				return true;
			}
		}
		return false;
		//~
	}
	
	private boolean isCommonHoliday(String year,String month,String dayOfMonth){
		Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(dayOfMonth));
		if(calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||calendar.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
		 {
		  return true;
		 }
		return false;
	}
}
